// sm3hash.c  
#include <openssl/evp.h>  
#include "sm3hash.h"  

int sm3_hash(const unsigned char *message, size_t len, unsigned char *hash, unsigned int *hash_len)  
{  
    EVP_MD_CTX *md_ctx = NULL;  
    const EVP_MD *md = NULL;  
    int ret = -1; // 默认返回值为失败  

    md_ctx = EVP_MD_CTX_new();  
    if (md_ctx == NULL) {  
        return ret;  
    }  

    md = EVP_sm3();  
    if (md == NULL) {  
        EVP_MD_CTX_free(md_ctx);  
        return ret;  
    }  

    if (EVP_DigestInit_ex(md_ctx, md, NULL) != 1) {  
        goto cleanup;  
    }  

    if (EVP_DigestUpdate(md_ctx, message, len) != 1) {  
        goto cleanup;  
    }  

    if (EVP_DigestFinal_ex(md_ctx, hash, hash_len) != 1) {  
        goto cleanup;  
    }  

    ret = 0; // 成功  

cleanup:  
    EVP_MD_CTX_free(md_ctx);  
    return ret;  
}